package gui;

import java.awt.EventQueue;

import javax.swing.JFrame;

import client.Client;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JSplitPane;
import java.awt.BorderLayout;

import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.WindowConstants;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Dimension;
import javax.swing.JTextField;
import javax.swing.JToolBar;
//import javax.swing.event.ListDataEvent;
//import javax.swing.event.ListDataListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class ClientMainWindow {

	private JFrame frmClientMainWindow;
	private Client client;
	private String clientName;
	private JList list;
	private JButton btnLogin;
	private JButton btnLogout;
	private JButton btnAdd;
	private JButton btnRemove;
	private JButton btnChat;
	private JScrollPane jsPane;
	
	public static void openWindow(final Client client) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ClientMainWindow window = new ClientMainWindow(client);
					window.frmClientMainWindow.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		openWindow(null);
	}
	
	/**
	 * Create the application.
	 */
	public ClientMainWindow(Client client) {
		this.client = client;
		this.clientName = null;
		client.set(this);
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		
		DefaultListModel dlm;
		
		frmClientMainWindow = new JFrame();
		frmClientMainWindow.setTitle("Client Main Window");
		frmClientMainWindow.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				client.logout();
				client.close();
			}
		});
		frmClientMainWindow.setBounds(100, 100, 400, 472);
		frmClientMainWindow.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		
		dlm = new DefaultListModel();
		list = new JList(dlm);
		jsPane = new JScrollPane(list);
		frmClientMainWindow.getContentPane().add(jsPane, BorderLayout.CENTER);
		
		JToolBar toolBar = new JToolBar();
		frmClientMainWindow.getContentPane().add(toolBar, BorderLayout.NORTH);
		
		btnLogin = new JButton("Login");
		toolBar.add(btnLogin);
		
		btnLogout = new JButton("Logout");
		btnLogout.setEnabled(false);
		toolBar.add(btnLogout);
		
		btnAdd = new JButton("Add friend");
		btnAdd.setEnabled(false);
		toolBar.add(btnAdd);
		
		btnRemove = new JButton("Remove friend");
		btnRemove.setEnabled(false);
		toolBar.add(btnRemove);
		
		btnChat = new JButton("Chat");
		btnChat.setEnabled(false);
		toolBar.add(btnChat);
		
		
		list.addListSelectionListener(new ListSelectionListener() {
			
			@Override
			public void valueChanged(ListSelectionEvent arg0) {
				int index = list.getSelectedIndex();
				if (index >= 0) {
					btnRemove.setEnabled(true);
					btnChat.setEnabled(true);
				}
				else {
					btnRemove.setEnabled(false);
					btnChat.setEnabled(false);
				}
			}
		});
		
		
		btnLogin.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String name = JOptionPane.showInputDialog(null, "Please enter your nickname", "");
				ClientMainWindow.this.clientName = name;
				if (name != null) {
					if (client.login(name) == true) {
						login(name);

					} else if (name.equals("")) {
						JOptionPane.showMessageDialog(null, "You cannot use an empty nickname", "Login", JOptionPane.ERROR_MESSAGE);
					} else {
						//JOptionPane.showMessageDialog(null, "Could not login", "Login", JOptionPane.ERROR_MESSAGE);
					}
				}
			}
		});
		
		
		
		btnLogout.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (client.logout() == false) {
					JOptionPane.showMessageDialog(null, "Could not logout", "Logout", JOptionPane.ERROR_MESSAGE);
				} else {
					/*
					((DefaultListModel)list.getModel()).removeAllElements();
					btnLogin.setEnabled(true);
					btnLogout.setEnabled(false);
					btnAdd.setEnabled(false);
					btnRemove.setEnabled(false);
					btnChat.setEnabled(false);
					frmClientMainWindow.setTitle("Client Main Window");
					*/
					logout();
				}
			}
		});
		
		
		btnAdd.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String name = JOptionPane.showInputDialog(null, "Please enter the friend's name", "");
				if (((DefaultListModel)list.getModel()).contains(name)) {
					JOptionPane.showMessageDialog(null, "A friend connected with this name has been already added", "AddFriend", JOptionPane.ERROR_MESSAGE);
				} else {
					((DefaultListModel)list.getModel()).addElement(name);
					list.validate();
					list.repaint();
				}
			}
		});
		
		btnRemove.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int index = list.getSelectedIndex();
				if (index < 0) {
					JOptionPane.showMessageDialog(null, "There is no selected friend", "RemoveFriend", JOptionPane.ERROR_MESSAGE);
					return;
				}

				((DefaultListModel)list.getModel()).remove(index);
				list.validate();
				list.repaint();
				
			}
		});
		
		btnChat.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int index = list.getSelectedIndex();
				if (index < 0)
					return;
				String name = (String)((DefaultListModel)list.getModel()).elementAt(index);
				client.sendCreateRoomMessage(name);
			}
		});
	}
	
	String[] getFriends() {	
		String[] sa = new String[((DefaultListModel)list.getModel()).size()];
		((DefaultListModel)list.getModel()).copyInto(sa);
		return sa;
	}
	
	public void logout() {
		
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				((DefaultListModel)list.getModel()).removeAllElements();
				btnLogin.setEnabled(true);
				btnLogout.setEnabled(false);
				btnAdd.setEnabled(false);
				btnRemove.setEnabled(false);
				btnChat.setEnabled(false);
				
				frmClientMainWindow.setTitle("Client Main Window");
				//JOptionPane.showMessageDialog(null, "You have been logged out", "Chat", JOptionPane.ERROR_MESSAGE);
			}
		});
	}
	
	public void login(final String name) {
		
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				btnLogin.setEnabled(false);
				btnLogout.setEnabled(true);
				btnAdd.setEnabled(true);
				frmClientMainWindow.setTitle("Client Main Window - " + name);
			}
		});
	}
	
	public String getClientName() {
		return clientName;
	}

}
